<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户管理</title>
    <link rel="stylesheet" href="/static/admin/component/pear/css/pear.css" />
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
</script>

<script type="text/html" id="user-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
</script>

<script type="text/html" id="user-enable">
    <input type="checkbox" name="enable" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="user-enable" checked = "{{ d.enable == 0 ? 'true' : 'false' }}">
</script>

<script src="/static/admin/component/layui/layui.js"></script>
<script src="/static/admin/component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery','common'], function() {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;

        let MODULE_PATH = "operate/";

        let cols = [
            [{
                type: 'checkbox'
            },
                {
                    title: '作者',
                    field: 'author',
                    align: 'center',
                    width: 100
                },
                {
                    title: '标题',
                    field: 'title',
                    align: 'center',
                    width: 100
                },
                {
                    title: '头像',
                    field: 'avatar',
                    align: 'center',
                    width: 100
                },
                {
                    title: '简介',
                    field: 'content',
                    align: 'center',
                    width: 100
                },
                {
                    title: '内容',
                    field: 'detail',
                    align: 'center',
                    width: 100
                },
                {
                    title: '外图片',
                    field: 'imgSrc',
                    align: 'center',
                    templet: '#user-login',
                    width: 100
                },
                {
                    title: '内图片',
                    field: 'headImgSrc',
                    align: 'center',
                    templet: '#user-createTime',
                    width: 100
                },
                {
                    title: '浏览',
                    field: 'reading',
                    align: 'center',
                    templet: '#user-createTime',
                    width: 100
                },
                {
                    title: '收藏',
                    field: 'collection',
                    align: 'center',
                    templet: '#user-createTime',
                    width: 100
                },
                {
                    title: '状态',
                    field: 'status',
                    align: 'center',
                    templet: '#user-enable',
                    width: 100
                },
                {
                    title: '操作',
                    toolbar: '#user-bar',
                    align: 'center',
                    width: 100
                }
            ]
        ];

        table.render({
            elem: '#user-table',
            url: '{:url("/admin/news/queryNews")}',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        table.on('tool(user-table)', function(obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        table.on('toolbar(user-table)', function(obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            }
        });

        form.on('submit(user-query)', function(data) {
            table.reload('user-table', {
                where: data.field
            })
            return false;
        });

        form.on('switch(user-enable)', function(obj) {
            layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
        });

        window.add = function() {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: [common.isModile()?'100%':'100%', common.isModile()?'100%':'100%'],
                content:'{:url("/admin/news/add")}'
            });
        }

        window.edit = function(obj) {
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: [common.isModile()?'100%':'100%', common.isModile()?'100%':'100%'],
                content:'{:url("/admin/news/edit")}?id='+obj.data['id']
            });
        }

        window.remove = function(obj) {
            layer.confirm('确定要删除该文章？', {
                icon: 3,
                title: '提示'
            }, function() {
                $.ajax({
                    url:  '{:url("/admin/news/delete_news")}',
                    dataType: 'json',
                    data:{
                        id : obj.data['id']
                    },
                    type: 'post',
                    success (res) {
                        if (res.code == 1) {
                            layer.msg(res.msg, {
                                icon: 6,
                                time: 1000
                            });
                            window.location.href = 'index.html';
                        } else {
                            layer.msg(res.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.batchRemove = function(obj) {

            var checkIds = common.checkField(obj,'userId');

            if (checkIds === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }

            layer.confirm('确定要删除这些用户', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "batchRemove/" + ids,
                    dataType: 'json',
                    type: 'delete',
                    success: function(result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function() {
                                table.reload('user-table');
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.refresh = function(param) {
            table.reload('user-table');
        }
    })
</script>
</body>
</html>
